<?php
namespace app\index\controller\user;

use app\index\controller\Base;

class Role extends Base
{
    public $tableName = "Role";
    public function index()
    {
        $this->page();
        return view();
    }

    /**
     *  添加
     */
    public function add()
    {
        $model = model('Role');
        if (request()->instance()->isPost()) {
            $post = request()->instance()->param();
         
            $data["name"] = $post["name"];

            $RoleModel = model("Role");
            $result = $RoleModel->save($data);
            if (empty($result)) {
                $this->error("操作失败");

            }
            $this->success("操作成功", url("/Index/User/Role/index"));
        }

        return view();
    }

    /**
     *  修改
     */
    public function edit()
    {
        $RoleModel = model('Role');
        if (request()->instance()->isPost()) {
            $post = request()->instance()->param();
          
            $data["name"] = $post["name"];          

            $map["id"] = $post["id"];
            $result = $RoleModel->save($data, $map);
            if (empty($result)) {
                $this->error("操作失败");

            }
            $this->success("操作成功", url("/Index/User/Role/index"));
        }

        $id = input("id");

        $data["info"] = $RoleModel->get($id);

        $this->assign($data);
        return view();
    }

    public function del()
    {
        $id = input("id");
        if (empty($id)) {
            $this->error("删除失败");
        }
        $RoleModel = model('Role');
        $result = $RoleModel->where('id', intval($id))->delete();
        if ($result) {
            $this->success("删除成功");
        }
        $this->error("删除失败");
    }
    //设置角色项目权限
    public function   setproject(){
        $id=input("id");
        $result=model("Role")->where(["id"=>intval($id)])->setField("is_project",1);
        if (!empty($result)) {
            $this->success("操作成功");
        }
        $this->error("操作失败");
    }

      //设置角色项目权限
    public  function unsetproject(){
        $id=input("id");
        $result=model("Role")->where(["id"=>intval($id)])->setField("is_project",0);
        if (!empty($result)) {
            $this->success("操作成功");
        }
        $this->error("操作失败");
    }
}
